package Module;

import Communication.TaskForModule;
import Communication.TaskForServer;
import LiftEngine.Lift;
import java.net.InetAddress;
import java.net.Socket;
import java.util.LinkedList;

/**
 *
 * Class description goes here.
 *
 * @author Kamil Gajowy
 */
public class ModuleClient extends Module{
    private int moduleType;
    private int id;
    private Socket socket;
    private boolean connected;
    private InetAddress destAddress;
    private int port;
    private String hostname;
    public static int currentTaskNumber = 0;



    private int taskLastDoneByServer = -1;

    public static int getCurrentTaskNumber() {
        return currentTaskNumber;
    }

    public static void setCurrentTaskNumber(int currentTaskNumber) {
        ModuleClient.currentTaskNumber = currentTaskNumber;
    }


    public InetAddress getDestAddress() {
        return destAddress;
    }

    public void setDestAddress(InetAddress destAddress) {
        this.destAddress = destAddress;
    }

    private LinkedList<TaskForModule> taskList = new LinkedList<TaskForModule>();
    private LinkedList<TaskForServer> serverTask = new LinkedList<TaskForServer>();
    private LinkedList<TaskForModule> taskDone = new LinkedList<TaskForModule>();

    public LinkedList<TaskForModule> getTaskDone() {
        return taskDone;
    }

    public void setTaskDone(LinkedList<TaskForModule> taskDone) {
        this.taskDone = taskDone;
    }

    public LinkedList<TaskForServer> getServerTask() {
        return serverTask;
    }

    public void setServerTask(LinkedList<TaskForServer> serverTask) {
        this.serverTask = serverTask;
    }

    public LinkedList<TaskForModule> getTaskList() {
        return taskList;
    }

    public void setTaskList(LinkedList<TaskForModule> taskList) {
        this.taskList = taskList;
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    /**
     * Get the value of port
     *
     * @return the value of port
     */
    public int getPort() {
        return port;
    }

    /**
     * Set the value of port
     *
     * @param port new value of port
     */
    public void setPort(int port) {
        this.port = port;
    }



    /**
    * Default constructor.
     * @param sock
     * @param id 
     */
    public ModuleClient(Socket sock, int id, int moduleType, Lift lift, int Type){
        super(lift, moduleType);
        this.socket = sock;
        this.id = id;
        this.moduleType = moduleType;
    }

    /**
     * @return the socket
     */
    public Socket getSocket() {
        return socket;
    }

    /**
     * @return the connected
     */
    public boolean isConnected() {
        return connected;
    }

    /**
     * @param connected the connected to set
     */
    public void setConnected(boolean connected) {
        this.connected = connected;
    }

    /**
     * @return the moduleType
     */
    public int getModuleType() {
        return moduleType;
    }

    /**
     * @param moduleType the moduleType to set
     */
    public void setModuleType(int moduleType) {
        this.moduleType = moduleType;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    public void setNewList()
    {
        this.serverTask = new LinkedList<TaskForServer>();
    }

    public void initSocket()
    {

        try{
            destAddress = InetAddress.getByName(hostname); // Destination address
            int destPort = getPort();              // Destination port
            this.socket = new Socket(destAddress, destPort);
            this.setConnected(true);
            System.out.println("done.");
        }
        catch (Exception ex)
        {
            System.out.println("failed.");
        }

    }

    void setLastDoneID(int lastDoneTaskID) {
        this.taskLastDoneByServer = lastDoneTaskID;
    }

    /**
     * @return the lastDoneTaskID
     */
    public int getLastTaskDoneByServer() {
        return taskLastDoneByServer;
    }


}//end class
